//
//  DHSubscription_CategoryView.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/6/8.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHSubscription_CategoryView.h"
#import "DHHomeHotBrandsCell.h"
#import "DHSubscription_CategoryView_CategoryCell.h"

@interface DHSubscription_CategoryView()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UIButton *manButton;
@property (weak, nonatomic) IBOutlet UIButton *womanButton;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) NSArray *brandsArray; //热门品牌
@property (strong, nonatomic) NSMutableArray *categoryArray;

@property (strong, nonatomic) NSString *sex;

@end

@implementation DHSubscription_CategoryView

+ (DHSubscription_CategoryView *)shareDHSubscription_CategoryView{
    DHSubscription_CategoryView *view = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([DHSubscription_CategoryView class]) owner:self options:nil] objectAtIndex:0];
    view.sex = @"男";
    return view;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    [self.manButton bk_addEventHandler:^(id sender) {
        [self.manButton setImage:[UIImage imageNamed:@"selected"] forState:UIControlStateNormal];
        [self.womanButton setImage:[UIImage imageNamed:@"unselected"] forState:UIControlStateNormal];
        self.sex = @"男";
        [self loadCategories:self.sex];
    } forControlEvents:UIControlEventTouchUpInside];
    [self.womanButton bk_addEventHandler:^(id sender) {
        [self.womanButton setImage:[UIImage imageNamed:@"selected"] forState:UIControlStateNormal];
        [self.manButton setImage:[UIImage imageNamed:@"unselected"] forState:UIControlStateNormal];
        self.sex = @"女";
        [self loadCategories:self.sex];
    } forControlEvents:UIControlEventTouchUpInside];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"DHHomeHotBrandsCell" bundle:nil] forCellReuseIdentifier:kDHHomeHotBrandsCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"DHSubscription_CategoryView_CategoryCell" bundle:nil] forCellReuseIdentifier:kDHSubscription_CategoryView_CategoryCell];
}

- (void)loadData{
    [self loadBrand];
    [self loadCategories:self.sex];
}

// 获取所有品类
- (void)loadCategories:(NSString *)sex{
    [[DHNetAPIClient shareDHNetAPIClient] request_categoriesWithParams:@{@"sex":sex} andBlock:^(id data, NSError *error) {
        [self.categoryArray removeAllObjects];
        
        NSMutableArray *arr = [NSMutableArray array];
        NSArray *tempArray = [data objectForKey:@"categories"];
        NSArray *indexArray = [tempArray valueForKey:@"type"];
        NSSet *indexSet = [NSSet setWithArray:indexArray];
        [[indexSet allObjects] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSPredicate *predicate = [NSPredicate predicateWithFormat:@"type == %@",obj];
            NSArray *indexArray = [tempArray filteredArrayUsingPredicate:predicate];
            [arr addObject:indexArray];
        }];
        [self.categoryArray addObjectsFromArray:[[arr reverseObjectEnumerator] allObjects]];
        [self.tableView reloadData];
    }];
}

- (void)loadBrand{
    //热门品牌
    [[DHNetAPIClient shareDHNetAPIClient] request_brands_hotWithParams:@{} AndBlock:^(id data, NSError *error) {
        if (data) {
            self.brandsArray = [data objectForKey:@"brands"];
            [self.tableView reloadData];
        }
    }];
}

#pragma mark - UITableViewDataSource and UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.categoryArray.count + 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == self.categoryArray.count) {
        if (self.brandsArray.count % 2 == 0 ) {
            return 87 + (self.brandsArray.count / 2) * 55;
        }else{
            return 87 + (self.brandsArray.count / 2 + 1) * 55;
        }
    }else{
        NSArray *arr = [self.categoryArray objectAtIndex:indexPath.row];
        if (arr.count % 4 == 0) {
            return (40 + (arr.count / 4) * 150);
        }else{
            return (40 + (arr.count / 4 + 1) * 150);
        }
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == self.categoryArray.count) {
        DHHomeHotBrandsCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHHomeHotBrandsCell forIndexPath:indexPath];
        [cell updateCellWithArray:self.brandsArray];
        return cell;
    }else{
        DHSubscription_CategoryView_CategoryCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHSubscription_CategoryView_CategoryCell forIndexPath:indexPath];
        NSArray *arr = self.categoryArray[indexPath.row];
        if (arr.count > 0) {
            NSDictionary *dic = [arr objectAtIndex:0];
            cell.titleLabel.text = [dic objectForKey:@"type"];
        }
        [cell updateCellArray:arr];
        return cell;
    }
}

#pragma mark - arr
- (NSArray *)brandsArray{
    if (!_brandsArray) {
        _brandsArray = [NSArray array];
    }
    return _brandsArray;
}

- (NSMutableArray *)categoryArray{
    if (!_categoryArray) {
        _categoryArray = [NSMutableArray array];
    }
    return _categoryArray;
}

@end
